package com.painpointcloud.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JackLeonJsonUtil {

	
//	public static void main(String[] args) {
//		
//		//定义一个Map
//		Map<String,Object> dataMap = new HashMap<String,Object>();
//		dataMap.put("tag", "data");
//		dataMap.put("hospitalName", "宜都市妇幼保健院");
//		dataMap.put("name", "欧阳夏凡");
//		dataMap.put("gender", "女");
//		dataMap.put("age", "28");
//		dataMap.put("code", "420502042");
//		dataMap.put("examineDoc", "杨林");
//		dataMap.put("examineDate", "2016-05-10");
//		dataMap.put("verifyDoc", "王菲");
//		System.out.println("初始化的Map对象："+dataMap);
//		//对象转Json字符串
//		String jsonStr=obj2JsonString(dataMap);
//		System.out.println(jsonStr);
//		Map<String,Object> returnMap=json2Map(jsonStr);
//		System.out.println("Json字符串转Map："+returnMap);
//	}
	
	public static void main(String[] args) throws Exception {
		//String s="[{\"flag\":\"menu\",\"name\":\"系统管理\",\"pId\":0,\"id\":\"1\"},{\"flag\":\"68\",\"name\":\"人工审核\",\"pId\":27,\"id\":\"field\"}]";
		//String s="[{id:1,flag:menu},{id:18,flag:menu},{id:19,flag:menu}]";
		String s="[{\"id\":\"1\",\"flag\":\"menu\"},{\"id\":\"18\",\"flag\":\"menu\"},{\"id\":\"19\",\"flag\":\"menu\"},{\"id\":\"20\",\"flag\":\"menu\"},{\"id\":\"21\",\"flag\":\"menu\"},{\"id\":\"field\",\"flag\":\"1\"},{\"id\":\"field\",\"flag\":\"2\"},{\"id\":\"field\",\"flag\":\"3\"},{\"id\":\"field\",\"flag\":\"4\"},{\"id\":\"field\",\"flag\":\"5\"},{\"id\":\"field\",\"flag\":\"6\"}]";
		List<Map<String,Object>> listMap=jsonArrayToMapList(s);
		for (Map map:
		listMap) {
			System.out.println("flag:"+map.get("flag")+",id:"+map.get("id"));
		}
		//String key="data.pyAntiFraudReport.personRiskAssess.checkResult";
		//四级
		//String access="{\"code\":\"0\",\"data\":{\"pyAntiFraudReport\":{\"fraudRiskInfo\":{\"errorMessage\":\"\",\"state\":\"0\",\"subReportType\":14229,\"subReportTypeCost\":96040,\"treatErrorCode\":\"\",\"treatResult\":1},\"microNearlyThreeYearsOverdueInfo\":{\"errorMessage\":\"\",\"items\":null,\"stat\":null,\"subReportType\":13204,\"subReportTypeCost\":96040,\"treatErrorCode\":\"\",\"treatResult\":2},\"mobileCheckInfo\":{\"areaInfo\":\"\",\"documentNoCheckResult\":\"\",\"errorMessage\":\"\",\"nameCheckResult\":\"\",\"operator\":\"\",\"phoneCheckResult\":\"\",\"subReportType\":13612,\"subReportTypeCost\":96040,\"treatErrorCode\":\"\",\"treatResult\":2},\"mobileStatusInfo\":{\"areaInfo\":\"\",\"cancelTime\":\"\",\"errorMessage\":\"\",\"operator\":\"\",\"phoneStatus\":\"\",\"subReportType\":13611,\"subReportTypeCost\":96040,\"timeLength\":\"\",\"treatErrorCode\":\"\",\"treatResult\":2},\"personAntiSpoofingDescInfo\":{\"errorMessage\":\"\",\"personAntiSpoofingDesc\":\"\\n\\t\\t1、反欺诈风险评分为98分，风险等级为高，建议拒绝。\\n\\t\\t2、未命中羊毛党名单。\\n\\t\\t3、未命中欺诈风险名单。\\n\\t\\t4、命中高风险名单。\\n\\t\\t5、存在6条个人风险概要信息。\\n\\t\\t6、在近两年被机构查询过62次个人信息。\\n\\t\",\"subReportType\":14225,\"subReportTypeCost\":96040,\"treatErrorCode\":\"\",\"treatResult\":1},\"personAntiSpoofingInfo\":{\"errorMessage\":\"\",\"hitTypes\":\"高风险名单，个人风险信息，被机构查询信息\",\"riskLevel\":\"高\",\"riskScore\":98.0,\"subReportType\":14224,\"subReportTypeCost\":96040,\"suggest\":\"建议拒绝\",\"treatErrorCode\":\"\",\"treatResult\":1},\"personRiskAssess\":{\"checkResult\":\"1\",\"errorMessage\":\"\",\"subReportType\":14226,\"subReportTypeCost\":96040,\"treatErrorCode\":\"\",\"treatResult\":1},\"personRiskInfo\":{\"als\":[{\"ajlx\":\"民事判决书\",\"bt\":\"原告李四与被告张三买卖合同纠纷一案\",\"dsrlx\":\"\",\"recordId\":\"al_329576D41D18491481\",\"sjnf\":\"2009\"}],\"cqs\":[{\"bt\":\"被催欠人测试五二\",\"fbrq\":\"2014-01-23\",\"recordId\":\"cq_10315D45D0\"}],\"errorMessage\":\"\",\"stat\":{\"alCount\":1,\"cqggCount\":1,\"swCount\":1,\"sxCount\":1,\"totleCount\":6,\"wdyqCount\":1,\"zxCount\":1},\"subReportType\":14227,\"subReportTypeCost\":96040,\"sws\":[{\"bt\":\"测试五二(非正常户)\",\"ggrq\":\"2012-08-01\",\"recordId\":\"sw_1403038D44D0\"}],\"sxs\":[{\"bt\":\"被执行人张三\",\"fbrq\":\"\",\"larq\":\"2009-02-06\",\"recordId\":\"zx_4436174D43D0\"}],\"treatErrorCode\":\"\",\"treatResult\":1,\"wdyqs\":[{\"bt\":\"测试五二\",\"fbrq\":\"2013-07-16\",\"recordId\":\"wdyq_4558264D46D0\"}],\"zxs\":[{\"bt\":\"被执行人张三\",\"larq\":\"2009-02-06\",\"recordId\":\"zx_4436174D43D0\",\"zxbd\":\"118200\"}]},\"policeCheckInfo\":{\"documentNo\":\"110000199001011112\",\"errorMessage\":\"\",\"name\":\"测试一\",\"result\":1,\"subReportType\":10602,\"subReportTypeCost\":96040,\"treatErrorCode\":\"\",\"treatResult\":1}},\"pyBankCardReport\":{\"personBankCheckInfo\":{\"personBankChecks\":[{\"accountBankName\":\"招商银行\",\"accountBankNo\":\"308584001016\",\"accountNo\":\"6226095950521497\",\"checkType\":\"姓名、证件号码和卡号\",\"documentNo\":\"110000199001011112\",\"errorMessage\":\"\",\"name\":\"测试一\",\"status\":1,\"subReportType\":14501,\"subReportTypeCost\":14501,\"treatErrorCode\":\"\",\"treatResult\":1}],\"status\":1}},\"tdAntiFraudReport\":\"\"},\"message\":\"成功\"}";
		//String access="{\"content\":{\"name\":\"张三\",\"age\":80,\"sex\":\"男\",\"hobby\":\"赌博\",\"others\":{\"blog_tyep\":\"新浪微博\",\"father\":{\"big\":\"齐齐哈萨克斯坦\",\"small\":\"萨布兰斯琦\"}}},\"houseData\":{\"guid\":\"38492a34-829c-46fc-b51f-a0091cf257b9\",\"rent\":0,\"price\":50777,\"totalprice\":600}}";
		//三级
		//String access="{\"content\":{\"name\":\"张三\",\"age\":80,\"sex\":\"男\",\"hobby\":\"赌博\",\"othres\":{\"blog_type\":\"新浪微博\",\"father\":\"齐齐哈萨克斯坦\"}},\"houseData\":{\"guid\":\"38492a34-829c-46fc-b51f-a0091cf257b9\",\"rent\":0,\"price\":50777,\"totalprice\":600}}";
		//String access="{\"content\":{\"name\":\"张三\",\"age\":80,\"sex\":\"男\",\"hobby\":\"赌博\"},\"houseData\":{\"guid\":\"38492a34-829c-46fc-b51f-a0091cf257b9\",\"rent\":0,\"price\":50777,\"totalprice\":600}}";
		//String access="{\"success\":true,\"risk_items\":[{\"risk_level\":\"high\",\"item_detail\":{\"namelist_hit_details\":[{\"hit_type_displayname\":\"借款人身份证\",\"fraud_type\":\"法院失信\",\"court_details\":[{\"gender\":\"男\",\"fraud_type\":\"法院失信\",\"filing_time\":\"2014年01月16日\",\"discredit_detail\":\"其他有履行能力而拒不履行生效法律文书确定义务\",\"execution_base\":\"(2013)清民初字第911号\",\"execution_department\":\"清流县人民法院\",\"province\":\"福建\",\"name\":\"李志标\",\"duty\":\"案件受理费350元，减半收取175元，由被告李志标负担。\",\"case_number\":\"(2014)清执行字第00037号\",\"court_name\":\"清流县人民法院\",\"age\":\"43\",\"situation\":\"全部未履行\"}],\"description\":\"身份证命中法院失信名单\",\"type\":\"black_list\"}],\"fraud_type\":\"法院失信\",\"court_details\":[{\"gender\":\"男\",\"fraud_type\":\"法院失信\",\"filing_time\":\"2014年01月16日\",\"discredit_detail\":\"其他有履行能力而拒不履行生效法律文书确定义务\",\"execution_base\":\"(2013)清民初字第911号\",\"execution_department\":\"清流县人民法院\",\"province\":\"福建\",\"name\":\"李志标\",\"duty\":\"案件受理费350元，减半收取175元，由被告李志标负担。\",\"case_number\":\"(2014)清执行字第00037号\",\"court_name\":\"清流县人民法院\",\"age\":\"43\",\"situation\":\"全部未履行\"}]},\"item_id\":1519349,\"item_name\":\"身份证命中法院失信名单\",\"group\":\"不良信息扫描\"},{\"risk_level\":\"high\",\"item_detail\":{\"namelist_hit_details\":[{\"hit_type_displayname\":\"借款人身份证\",\"fraud_type\":\"刑事犯罪\",\"description\":\"身份证命中犯罪通缉名单\",\"type\":\"black_list\"}],\"fraud_type\":\"刑事犯罪\"},\"item_id\":1519351,\"item_name\":\"身份证命中犯罪通缉名单\",\"group\":\"不良信息扫描\"},{\"risk_level\":\"high\",\"item_detail\":{\"namelist_hit_details\":[{\"hit_type_displayname\":\"借款人身份证\",\"fraud_type\":\"法院执行\",\"court_details\":[{\"execution_status\":\"-\",\"fraud_type\":\"法院执行\",\"name\":\"秦志良\",\"filing_time\":\"2015年08月24 00:00:00日\",\"execution_number\":\"-\",\"case_number\":\"(2015)即调执字第1316号\",\"court_name\":\"即墨市人民法院\"}],\"description\":\"身份证命中法院执行名单\",\"type\":\"black_list\"}],\"fraud_type\":\"法院执行\",\"court_details\":[{\"execution_status\":\"-\",\"fraud_type\":\"法院执行\",\"name\":\"秦志良\",\"filing_time\":\"2015年08月24 00:00:00日\",\"execution_number\":\"-\",\"case_number\":\"(2015)即调执字第1316号\",\"court_name\":\"即墨市人民法院\"}]},\"item_id\":1519353,\"item_name\":\"身份证命中法院执行名单\",\"group\":\"不良信息扫描\"},{\"risk_level\":\"high\",\"item_detail\":{\"namelist_hit_details\":[{\"hit_type_displayname\":\"借款人身份证\",\"fraud_type\":\"助学贷款逾期\",\"description\":\"身份证对应人存在助学贷款欠费历史\",\"type\":\"black_list\"}],\"fraud_type\":\"助学贷款逾期\"},\"item_id\":1519355,\"item_name\":\"身份证对应人存在助学贷款欠费历史\",\"group\":\"不良信息扫描\"},{\"risk_level\":\"high\",\"item_detail\":{\"discredit_times\":1,\"overdue_details\":[{\"overdue_amount\":31725,\"overdue_day\":602,\"overdue_count\":3}],\"type\":\"discredit_count\"},\"item_id\":1519357,\"item_name\":\"身份证命中信贷逾期名单\",\"group\":\"不良信息扫描\"},{\"risk_level\":\"medium\",\"item_detail\":{\"namelist_hit_details\":[{\"hit_type_displayname\":\"借款人身份证\",\"fraud_type\":\"失信名单\",\"description\":\"身份证命中高风险关注名单\",\"type\":\"grey_list\"}],\"fraud_type\":\"失信名单\"},\"item_id\":1519359,\"item_name\":\"身份证命中高风险关注名单\",\"group\":\"不良信息扫描\"},{\"risk_level\":\"high\",\"item_detail\":{\"namelist_hit_details\":[{\"hit_type_displayname\":\"借款人身份证\",\"fraud_type\":\"汽车租赁欺诈\",\"description\":\"身份证命中车辆租赁违约名单\",\"type\":\"black_list\"}],\"fraud_type\":\"汽车租赁欺诈\"},\"item_id\":1519361,\"item_name\":\"身份证命中车辆租赁违约名单\",\"group\":\"不良信息扫描\"},{\"risk_level\":\"medium\",\"item_detail\":{\"namelist_hit_details\":[{\"hit_type_displayname\":\"借款人身份证\",\"fraud_type\":\"法院结案、法院结案\",\"court_details\":[{\"gender\":\"-\",\"fraud_type\":\"法院结案\",\"filing_time\":\"2010年04月09日\",\"execution_number\":\"79.5\",\"discredit_detail\":\"-\",\"execution_base\":\"-\",\"execution_department\":\"-\",\"province\":\"-\",\"execution_status\":\"已结案\",\"name\":\"程克强\",\"duty\":\"-\",\"case_number\":\"(2010)松执字第01860号\",\"court_name\":\"上海市松江区人民法院\",\"age\":\"-\",\"situation\":\"-\"},{\"gender\":\"男\",\"fraud_type\":\"法院结案\",\"filing_time\":\"2014年05月23日\",\"execution_number\":\"-\",\"discredit_detail\":\"违反财产报告制度,其他有履行能力而拒不履行生效法律文书确定义务\",\"execution_base\":\"（2013）潭仲裁字第273号\",\"execution_department\":\"湘潭仲裁委员会\",\"province\":\"湖南\",\"execution_status\":\"-\",\"name\":\"邓声波\",\"duty\":\"强制执行被申请人583400元。\",\"case_number\":\"(2014)潭中执字第00057号\",\"court_name\":\"湘潭市中级人民法院\",\"age\":\"55\",\"situation\":\"全部未履行\"}],\"description\":\"身份证命中法院结案名单\",\"type\":\"black_list\"}],\"fraud_type\":\"法院结案、法院结案\",\"court_details\":[{\"gender\":\"-\",\"fraud_type\":\"法院结案\",\"filing_time\":\"2010年04月09日\",\"execution_number\":\"79.5\",\"discredit_detail\":\"-\",\"execution_base\":\"-\",\"execution_department\":\"-\",\"province\":\"-\",\"execution_status\":\"已结案\",\"name\":\"程克强\",\"duty\":\"-\",\"case_number\":\"(2010)松执字第01860号\",\"court_name\":\"上海市松江区人民法院\",\"age\":\"-\",\"situation\":\"-\"},{\"gender\":\"男\",\"fraud_type\":\"法院结案\",\"filing_time\":\"2014年05月23日\",\"execution_number\":\"-\",\"discredit_detail\":\"违反财产报告制度,其他有履行能力而拒不履行生效法律文书确定义务\",\"execution_base\":\"（2013）潭仲裁字第273号\",\"execution_department\":\"湘潭仲裁委员会\",\"province\":\"湖南\",\"execution_status\":\"-\",\"name\":\"邓声波\",\"duty\":\"强制执行被申请人583400元。\",\"case_number\":\"(2014)潭中执字第00057号\",\"court_name\":\"湘潭市中级人民法院\",\"age\":\"55\",\"situation\":\"全部未履行\"}]},\"item_id\":1519363,\"item_name\":\"身份证命中法院结案名单\",\"group\":\"不良信息扫描\"},{\"risk_level\":\"low\",\"item_detail\":{\"frequency_detail_list\":[{\"data\":[\"18367458987\",\"138※※※※0000\"],\"detail\":\"3个月身份证关联手机号数：2\"},{\"detail\":\"3个月身份证关联邮箱数：0\"},{\"detail\":\"3个月身份证关联家庭地址数：0\"}],\"type\":\"frequency_detail\"},\"item_id\":1519497,\"item_name\":\"3个月内身份证关联多个申请信息\",\"group\":\"客户行为检测\"},{\"risk_level\":\"low\",\"item_detail\":{\"frequency_detail_list\":[{\"detail\":\"7天内手机号申请次数：8\"}],\"type\":\"frequency_detail\"},\"item_id\":1519521,\"item_name\":\"7天内设备或身份证或手机号申请次数过多\",\"group\":\"客户行为检测\"},{\"risk_level\":\"medium\",\"item_detail\":{\"platform_detail_dimension\":[{\"count\":2,\"detail\":[\"财产保险:1\",\"小额贷款公司:1\"],\"dimension\":\"借款人手机详情\"},{\"count\":2,\"detail\":[\"财产保险:1\",\"小额贷款公司:1\"],\"dimension\":\"借款人身份证详情\"}],\"platform_detail\":[\"财产保险:1\",\"小额贷款公司:1\"],\"platform_count\":2,\"type\":\"platform_detail\"},\"item_id\":1519561,\"item_name\":\"1个月内申请人在多个平台申请借款\",\"group\":\"多平台借贷申请检测\"},{\"risk_level\":\"medium\",\"item_detail\":{\"platform_detail_dimension\":[{\"count\":2,\"detail\":[\"财产保险:1\",\"小额贷款公司:1\"],\"dimension\":\"借款人手机详情\"},{\"count\":2,\"detail\":[\"财产保险:1\",\"小额贷款公司:1\"],\"dimension\":\"借款人身份证详情\"}],\"platform_detail\":[\"财产保险:1\",\"小额贷款公司:1\"],\"platform_count\":2,\"type\":\"platform_detail\"},\"item_id\":1519563,\"item_name\":\"3个月内申请人在多个平台申请借款\",\"group\":\"多平台借贷申请检测\"},{\"risk_level\":\"medium\",\"item_detail\":{\"frequency_detail_list\":[{\"detail\":\"1小时内身份证申请次数：2\"},{\"detail\":\"1天内身份证申请次数：8\"}],\"type\":\"frequency_detail\"},\"item_id\":1519467,\"item_name\":\"1天内身份证或手机号申请次数大于等于5且1小时内身份证或手机号申请次数小于3\",\"group\":\"客户行为检测\"},{\"risk_level\":\"medium\",\"item_detail\":{\"frequency_detail_list\":[{\"detail\":\"1小时内手机号申请次数：2\"},{\"detail\":\"1天内手机号申请次数：8\"}],\"type\":\"frequency_detail\"},\"item_id\":1519491,\"item_name\":\"1天内身份证或手机号关联的设备数大于等于5且1小时内身份证或手机号关联的设备数小于3\",\"group\":\"客户行为检测\"},{\"risk_level\":\"medium\",\"item_id\":1519499,\"item_name\":\"1个月内同一个手机号码申请被拒次数大于等于4\",\"group\":\"客户行为检测\"}],\"report_id\":\"ER2017061309320117309181\",\"final_score\":100,\"final_decision\":\"Reject\",\"report_time\":1497317522000,\"application_id\":\"170613093201828B32A0A59F1C6455C9\",\"apply_time\":1497317522000,\"address_detect\":{\"mobile_address\":\"浙江省宁波市\",\"id_card_address\":\"吉林省通化市通化县\"}}";
//		String value=getValueByKeyFromJson(key,access);
//		System.out.println(value);
	
		}
	/**
	 * 从Json中通过多级key取值，key的格式如：key1.key2.key3...
	 * @param key
	 * @param json
	 * @return value
	 */
	 public static String getValueByKeyFromJson(String key,String json){
		 String value="";
		 Map<String,Object> returnMap=new HashMap<String,Object>();
		 if(JackLeonStringUtil.isNotEmpty(json)&&JackLeonStringUtil.isNotEmpty(key)){
			 Map<String,Object> map=JackLeonJsonUtil.json2Map(json);
			 String[] strArray=JackLeonStringUtil.splitStringByCharacter(key, ".");
			 for (int i = 0; i < strArray.length; i++) {
				if(i==0){
					returnMap=getMapByKeyFromMap(strArray[i],map);
				}else if(i==(strArray.length-1)){
					value= returnMap.get(strArray[i]).toString();
				}else{
					returnMap=getMapByKeyFromMap(strArray[i],returnMap);
				}
			}
		 }
		 return value;
	 }
	 /**
	  * 从一个Map中根据key取得子Map
	  * @param key
	  * @param map
	  * @return returnMap
	  */
	public static Map<String,Object> getMapByKeyFromMap(String key,Map<String,Object> map){
		Map<String,Object> returnMap=new HashMap<String,Object>();
		if(map.containsKey(key)){
			returnMap=(Map<String, Object>) map.get(key);
		    }
		return returnMap;
	}
	/**
	 * Json转Map对象
	 * @param json
	 * @return
	 */
	public static Map<String,Object> json2Map(String json){
		return JSON.parseObject(json, Map.class);
		}
	/**
	 * 对象转Json字符串
	 * @param obj
	 * @return
	 */
		public static String obj2JsonString(Object obj){
		return JSON.toJSONString(obj);
		}

	/**
	 * List<Map<String,Object>转Json
	 * @param list
	 * @return
	 */
	public static JSONArray ListMapToJason(List<Map<String,Object>> list) {
		JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(list));
		return jsonArray;
	}
	/**
	 * 功能：把json数组字符串转化为List<Map<String,Object>>类型
	 * @param jsonArrayString 
	 * @return List<Map<String,Object>>
	 */
	public static List<Map<String,Object>> jsonArrayToMapList(String jsonArrayString){
		JSONArray jsonArray=JSONObject.parseArray(jsonArrayString);
		List<Map<String,Object>> mapList = (List)jsonArray;
		return mapList;
	}
}
